#include "mypushbutton.h"
#include<QPropertyAnimation>


MyPushButton::MyPushButton(QString normalImg,QString pressImg)
{

    this->normalImgPath = normalImg;
    this->pressedImgPath = pressImg;

    //QPixmap加载图片
    QPixmap pix;
    bool ret = pix.load(normalImgPath);
    if(!ret)
    {
        QString srt = QString("图片加载失败").arg(normalImg);
        qDebug() << srt;
        return;
    }

    //设置图片的大小
    this->setFixedSize(pix.width(),pix.height());
    //设置不规则图片的样式
    this->setStyleSheet("border:0px groove gray;border-radius:10px;padding:2px 4px;");
    //设置图标
    this->setIcon(pix);
    //设置图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));


}

void MyPushButton::zoom1()
{
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(200);
    //设置起始位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置弹起效果
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //让动画执行
    animation->start(QAbstractAnimation::DeleteWhenStopped);

}

void MyPushButton::zoom2()
{
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(200);
    //设置起始位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置弹起效果
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //让动画执行
    animation->start(QAbstractAnimation::DeleteWhenStopped);

}

//鼠标事件
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    //选中路径不为空，显示选中图片
    if(pressedImgPath != "")
    {
        QPixmap pixmap;
        //判断是否加载成功
        bool ret = pixmap.load(pressedImgPath);
        if(!ret)
        {
            qDebug() << pressedImgPath << "加载图片失败!";
        }

        //修改图片样式
        this->setFixedSize( pixmap.width(), pixmap.height() );
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    }
    //交给父类执行按下事件
    return QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(normalImgPath != "") //选中路径不为空，显示选中图片
    {
        QPixmap pixmap;
        bool ret = pixmap.load(normalImgPath);
        if(!ret)
        {
            qDebug() << normalImgPath << "加载图片失败!";
        }
        this->setFixedSize( pixmap.width(), pixmap.height() );
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    }
    //交给父类执行 释放事件
    return QPushButton::mouseReleaseEvent(e);
}


